import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class login {
    static Map<String,String> usermessage = new HashMap<String,String>();

    public static void main(String[] args) {
        usermessage.put("admin","123456");
        JFrame frame = new JFrame("登录");
        frame.setLayout(new BorderLayout());
        frame.setSize(400,200);
        frame.setLocation(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JLabel username = new JLabel("用户名");
        JLabel password = new JLabel("密码");
        JTextField usertext = new JTextField(15);
        JPasswordField pswdtext = new JPasswordField(15);
        JButton login = new JButton("登录");
        JButton register = new JButton("注册");
        panel1.add(username);
        panel1.add(usertext);
        panel2.add(password);
        panel2.add(pswdtext);
        panel3.add(login);
        panel3.add(register);
        frame.add(panel1,BorderLayout.PAGE_START);
        frame.add(panel2,BorderLayout.CENTER);
        frame.add(panel3,BorderLayout.PAGE_END);
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog dialog = new JDialog(frame);
                dialog.setTitle("注册");
                dialog.setSize(400,200);
                dialog.setLocation(700,350);
                dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
                dialog.setLayout(new BorderLayout());
                JPanel panel1 = new JPanel();
                JPanel panel2 = new JPanel();
                JPanel panel3 = new JPanel();
                JLabel username = new JLabel("用户名");
                JLabel password = new JLabel("密码");
                JTextField usertext = new JTextField(15);
                JPasswordField pswdtext = new JPasswordField(15);
                JButton ok = new JButton("确定");
                panel1.add(username);
                panel2.add(password);
                panel1.add(usertext);
                panel2.add(pswdtext);
                panel3.add(ok);
                dialog.add(panel1,BorderLayout.PAGE_START);
                dialog.add(panel2,BorderLayout.CENTER);
                dialog.add(panel3,BorderLayout.PAGE_END);

                dialog.setVisible(true);
                ok.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String user= usertext.getText();
                        String pswd= pswdtext.getText();
                        usermessage.put(user,pswd);
                        dialog.setVisible(false);
                    }
                });

            }
        });
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String user= usertext.getText();
                String pswd= pswdtext.getText();
                int count = 0;
                for (Map.Entry<String,String> entry:usermessage.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if (user.equals(key)&&pswd.equals(value)){
                        JDialog dialog = new JDialog(frame);
                        dialog.setTitle("登录成功");
                        dialog.setSize(400,200);
                        dialog.setLocation(700,350);
                        dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
                        dialog.setVisible(true);

                        count = 1;
                        break;
                    }
                }
                if (count==0) {
                    JDialog dialog = new JDialog(frame);
                    dialog.setTitle("登录失败");
                    dialog.setSize(400,200);
                    dialog.setLocation(700,350);
                    dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
                    dialog.setVisible(true);

                }
            }
        });

        frame.setVisible(true);


    }
}
